 $(document).ready(function() {
             //		权限判断
		if($.cookie('ID') == null){
			window.location.href="http://www.duoweish.com/crm/login.html";
		}
//		2，3号权限
		if($.cookie('Right') == 1 ){
			window.location.href="http://www.duoweish.com/crm/index.html";
		}	
		$('.dw_name').text($.cookie('Name'));
		 $('.dw_part').text($.cookie('Department'));
 				
			 var page_num = 0;	
             
             
             init(1);
             
            function Ajax(curr){
				
            	$.ajax({
				headers:{
				    	Accept:"application/json;charset=utf-8"
				    },
					type:"get",
					url:"http://www.duoweish.com/apis/UserRight/",
					data:{
					page:curr||1,
					rows:12
					},
					success:function(data){
						if(data.length!=0){
							page_num = data[1];
						    tableData(data);
					     	divePage(page_num,curr);
						}else{
							layer.msg('没找到该商家')
						}
						
						}
				})
            	
            }
            function tableData(ret){
                    var option1;
                    $('#table_body').empty();
                    for(var i = 0;i < ret.length;i++){
                    	for (var j = 0;j<ret[i].length;j++) {
                    		
                    	if(ret[i][j][4]==1){
                    		 option1 = '<option selected="selected">1号权限</option><option>2号权限</option><option>3号权限</option><option>无权限</option>';
                    	}
                    	if(ret[i][j][4]==2){
                    		 option1 = '<option>1号权限</option><option selected="selected">2号权限</option><option>3号权限</option><option>无权限</option>';
                    	}
                    	if(ret[i][j][4]==3){
                    		 option1 = '<option>1号权限</option><option>2号权限</option><option selected="selected">3号权限</option><option>无权限</option>';
                    	}
                    	if(ret[i][j][4]==0){
                    		 option1 = '<option>1号权限</option><option>2号权限</option><option>3号权限</option><option selected="selected">无权限</option>';
                    	}
                        var tr = `<tr>
                                <td style="line-height:20px;">${ret[i][j][0]}</td>
                                <td style="line-height:20px;">${ret[i][j][1]}</td>
                                <td style="line-height:20px;">${ret[i][j][2]}</td>
                                <td style="line-height:20px;">${ret[i][j][3]}</td>
                                <td>
                                  <select class="look_ok">
                                  ${option1}
                                  </select>
                                </td>
                               <td index="${ret[i][j][5]}">
                                    <button class="look_ok check1" index="1"><i style="font-size: 20px;" class="fa fa-pencil"></i></button>
                                	<span style="display:inline-block;width:20px;"></span>
                                	<button class="look_ok check2" index="0"><i style="font-size: 20px;" class="fa fa-trash-o"></i></button>
                                
                                </td>
                            </tr>`;
                        $('#table_body').append(tr);
                        
                    }
                }
                   
                   
                
                 $('.check1').click(function(){
                 	        var  Auth = $(this).parent().parent().find("select").find("option:selected").text();
							var User_ID = $.cookie('ID');
							var num_ID = $(this).parent().parent().find("td").eq(0).text();
							if(User_ID!==null){
									$.ajax({
									type:"get",
									url:"http://www.duoweish.com/apis/RightEdit/",
									data:{
										UserId:User_ID,
										Id:num_ID ,
										Right: Auth
									},
									success:function(){
										layer.msg('修改成功')
			//							window.location.href="http://www.duoweish.com/crm/Author.html"
									}
								});
							}else{
			//					window.location.href="http://www.duoweish.com/crm/login.html"
								layer.msg('修改失败')
							}
                 })
                  $('.check2').click(function(){
                 	        var num_ID = $(this).parent().parent().find("td").eq(0).text();
						layer.confirm('是否要删除用户?',{skin:"demo-class"},function(index){
							$.ajax({
								type:"get",
								url:"http://www.duoweish.com/apis/UserDel/",
								data:{
									Id:num_ID,
								},
								success:function(data){
									if(data == "success"){
										layer.msg('删除成功');
										Ajax(1);								
									}else{
										layer.msg('删除失败，请重试');
									}							
								}
							})
						},function(index){
							layer.close(index)
						})
                 })
      
                    
            }
          
           function divePage(page_num,curr){
            layui.use(['laypage', 'layer'], function(){
					 var laypage = layui.laypage
			  		,layer = layui.layer;
			  	
                     laypage.render({
						    elem: 'demo0'
						    ,count: page_num
						    ,limit:12
						    ,curr:curr
						    ,jump: function(obj, first){
						    	   if(!first){
						                Ajax(obj.curr);
						               }
				        
						      		}
						   		});
					 })		
           }
          
           function init(curr){ 
           	     Ajax(1)
	            divePage(page_num);
           }
          
     
           
})